﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Linq;
using WPFExcelReport.Helpers;
using System.Collections;
using System.Collections.Generic;
using WPFExcelReport;
namespace INTEX.SCHEDULER
{

    public partial class SPAPS : ITXSSPA
    {
        public DataSet XSSchedule(object p1 = null, object p2 = null, object p3 = null, object p4 = null, object p5 = null)
        {
            HLATIME = DateTime.Now;
            HLAMinutes = 0;
            SetProd = (DataSet)p1;

            DataRow srow = null;
            DataRow nSchedule = null;
            string filter = "";
            string sort = "";
            //===初始化可排产任务=========

            try
            {
                string processed = "";
                ResetJob();
                //这里处理了已经确认或已经生成的任务，不允许移动位置
                //对于日历，也要进行相同的处理。
                //需要注意排序的规则和时间 
                //=====初始化设备=======
                ReSetMachine();
                int totalcount = TBSTDM.Rows.Count * TVKPJOBS.Rows.Count;
                int Runs = 0;
                #region======确定机型===========
                while (true)
                {
                    if (Runs > totalcount)
                        break;
                    Runs++;
                    filter = "ABAID='" + ABAID + "'";
                    rows = TVKPJOBS.Select(filter);
                    foreach (DataRow row in rows)
                    {
                        row.Delete();
                    }
                    TVKPJOBS.AcceptChanges();
                    nSchedule = null;
                    filter = "ABFBLFDNR='0000'";
                    sort = "JWDRATIO DESC,WDRATIO DESC,JDRATIO DESC,JWDNUM DESC,WDNUM DESC,JDNUM DESC,JWNUM ASC,WNUM ASC,JNUM ASC,RVKKLINES ASC,SVKKLINES ASC,VKKLINES ASC ,RGB ASC,ABFSOLLDAT ASC ,VKPLIEFTERM ASC,VKPNEUDAT ASC";
                    rows = TVKPJOBS.Select(filter, sort);
                    foreach (DataRow job in rows)
                    {
                        ABAID = job["ABAID"].ToString();
                        /*
                        if (processed.Contains(ABAID))
                        {
                            continue;
                        }
                        if (ABAID == "107906")
                        {
                        }
                        processed += ABAID;*/
                        EvaluateMachine(job);
                        DataRow dwMachine = null;
                        var qM = from DataRow stdm in TBSTDM.AsEnumerable()
                                 where Convert.ToDouble(stdm["JMVALUE"].ToString()) > 0
                                 orderby stdm.Field<Decimal>("JMVALUE") descending
                                 select stdm;
                        foreach (DataRow stdm in qM)
                        {
                            dwMachine = stdm;
                            break;
                        }
                        if (dwMachine == null)
                            continue;
                        job["MACHINEID"] = dwMachine["MACHINEID"];
                        job["MKFPLTIME"] = Runs;
                        srow = SIMULATIONUSEDJOBS.NewRow();
                        srow.ItemArray = job.ItemArray;
                        SIMULATIONUSEDJOBS.Rows.Add(srow);
                        nSchedule = srow;
                        UpdateAB(nSchedule);
                        UpdateArticle(nSchedule);
                        UpdateVKK(nSchedule);
                        break;
                    }
                }
                #endregion
                #region====分配机台=======
                List<Task> tasks = new List<Task>();
                foreach (DataRow dwMachine in TBSTDM.Rows)
                {
                    ScheduleMachineType(dwMachine);
                }
               // Task.WaitAll(tasks.ToArray());
                #endregion
            }
            catch (Exception ex)
            {

                LogHelper.error(ex.StackTrace + ex.Message);
            }
            TVKPJOBS.Clear();
            TVKPJOBS.Merge(SIMULATIONUSEDJOBS);
            VMK.SaveTask(SetData);
            return SetData;
        }
    }
}

